﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageWorkflow.Dispatch;

namespace MessageWorkflow.Dispatch
{
    /// <summary>
    /// Aggregates all exceptions together to eventually return an aggregate ex
    /// </summary>
    public class AggregateErrorStrategy : IErrorStrategy
    {
        private List<Exception> _foundExceptions;

        public bool OnError(Exception ex)
        {
            if (_foundExceptions == null)
            {
                _foundExceptions = new List<Exception>();
            }

            _foundExceptions.Add(ex);

            // continue working 
            return true;
        }

        public void OnComplete()
        {
            if (_foundExceptions != null)
            {
                Exception exception = new AggregateException("1 or more handlers raised an ex while dispatching", _foundExceptions);

                throw exception;
            }
        }
    }
}
